﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SystemAnalizer.Common;
using System.Windows.Controls;

namespace SystemAnalizer.Models
{
   public class PeriodicMatrixModel:SystemModel
    {
       private int period;
       
       public PeriodicMatrixModel(int discrete_num,int systemsize)
           : base(discrete_num, systemsize)
        {
        }

       public int GetPeriod
       {
           get { return this.period; }
       }

       public bool startMakeDiscretes()
       {
           StringBuilder errorstr = new StringBuilder();
           bool isok = true;
           double[] calcans;
           double[, ,] tempval = new double[100, this.system_size, this.system_size];
           this.discretemaker = new DiscreteMaker(this.m_CenterOfApproximation, this.H_step, 100, "t");

           for (int i = 0; i < this.system_size; i++)
           
               for (int j = 0; j < this.system_size; j++)
               {
                   try
                   {
                       calcans = this.discretemaker.calculate(this.A_matrix_input[i, j]);
                       if (calcans != null && calcans.Length > 0)
                       {
                           for (int l = 0; l < 100; l++)
                               tempval[l, i, j] = calcans[l];
                       }
                       else
                       {
                           errorstr.Append(string.Format("invalid input in row {0}, cell {1} \n", i, j));
                           isok = false;
                       }
                   }
                   catch (Exception ex)
                   {
                       errorstr.Append(string.Format("invalid input in row {0}, cell {1} \n", i, j));
                       isok = false;
                   }
               }

           if (!isok)
               ErrorDialogs.PrintErrorDialog(errorstr.ToString());
           else
           {
               CalculatePeriod(tempval);
               SetDiscrtes(tempval);
           }
           return isok;
       }

       public void SystemInserValtoContainerAdder(StackPanel container)
       {
           base.SystemInserValtoContainerAdder(container);
           Label period = new Label();
        //   period.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;
           period.Content = string.Format("Period={0}", this.period);
           container.Children.Add(period);
       }

       private void SetDiscrtes(double[, ,] tempval)
       {
           this.A_descretematrix = new double[this.number_discretes,this.system_size,this.system_size];
           for (int l = 0; l < this.number_discretes; l++)
               for (int i = 0; i < this.system_size; i++)
                   for (int j = 0; j < this.system_size; j++)
                       this.A_descretematrix[l, i, j] = tempval[l, i, j];

       }

       private void CalculatePeriod(double[, ,] tempval)
       {
           bool isper;
           for (int l = 1; l < 100; l++)
           {
                isper = true;
                for (int i = 0; i < this.system_size; i++)
                    for (int j = 0; j < this.system_size; j++)
                        if (tempval[0, i, j] != tempval[l, i, j])
                            isper = false;
                if (isper)
                    this.period = l;
           }
       }
    }
}
